package LeetCode.month01.day26;

public class No2134 {
    public static void main(String[] args) {
        System.out.println(minSwaps(new int[]{0,1,1,1,0,0,1,1,0}));
    }
    public static int minSwaps(int[] nums) {
        int len = nums.length;
        int k = 0;
        for (int i = 0; i < len; i++) {
            if(nums[i]==1) k++;
        }
        int num = 0;
        int res = 0;
        for (int i = 0; i < k; i++) {
            if(nums[i]==0) num++;
        }
        res = num;
        for (int i = k; i < len; i++) {
            if(nums[i]==0) num++;
            if(nums[i-k]==0) num--;
            res = Math.min(res,num);
        }
        for (int i = 0; i < k; i++) {
            if(nums[i]==0) num++;
            if(nums[len-k+i]==0) num--;
            res = Math.min(res,num);
        }
        return res;
    }
}
